<template>
  <div id="app" class="wrapper wrapper-content animated fadeInRight v-cloak" v-cloak>
    <div class="row">
      <div class="ibox float-e-margins">
        <div class="ibox-content col-md-12">
          <form id="riverStaffForm" method="post" class="form m-t" @submit.prevent="save()" accept-charset="UTF-8">
            <input type="hidden" name="id" v-model="item.id"/>
            <input type="hidden" name="token" v-model="item.token"/>
            <input type="hidden" name="newFlag" v-model="newFlag"/>
            <div class="clearfix "></div>
            <div class="col-md-6 ">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">人员名称：</label>
                <div class="col-md-12">
                  <input id="bean-name" name="name" v-model="item.name" class="form-control" type="text" placeholder="请输入人员名称" required="" aria-required="true" aria-describedby="bean-name-error" maxlength="255" aria-invalid="true">
                  <span id="bean-name-error" class="help-block m-b-none" for="bean-name"></span>
                </div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">人员类型：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择人员类型" id="bean-job" name="job" class="form-control" required="" aria-required="true" aria-describedby="bean-job-error" maxlength="55" aria-invalid="true">
                    <option :selected="item.job=='河长办人员'" value="河长办人员">河长办人员</option>
                    <option :selected="item.job=='巡查员'" value="巡查员">巡查员</option>
                    <option :selected="item.job=='监督员'" value="监督员">监督员</option>
                  </select>
                  <span id="bean-job-error" class="help-block m-b-none" for="bean-job"></span>
                </div>
              </div>
            </div>
            <div class="clearfix "></div>
            <div class="col-md-12">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">主要职责：</label>
                <div class="col-md-12">
                  <input id="bean-coreResponsibility" name="coreResponsibility" v-model="item.coreResponsibility" class="form-control" type="text" placeholder="请输入主要职责" aria-describedby="bean-coreResponsibility-error" maxlength="255" aria-invalid="true">
                  <span id="bean-coreResponsibility-error" class="help-block m-b-none" for="bean-coreResponsibility"></span>
                </div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">所属部门：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择所属部门" id="bean-orgId" name="orgId" class="form-control" aria-describedby="bean-orgId-error" aria-invalid="true">
                    <option value="">--请选择所属部门--</option>
                    <option :value="orgItem.id" :selected="item.orgId == orgItem.id" v-for="orgItem in orgItems">{{ orgItem.name }}</option>
                  </select>
                  <span id="bean-orgId-error" class="help-block m-b-none" for="bean-orgId"></span>
                </div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">所在河道：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择所在河道" id="bean-riverId" name="riverId" class="form-control" aria-describedby="bean-riverId-error" required="" aria-invalid="true">
                    <option value="">--请选择所在河道--</option>
                    <option :value="river.id" :selected="item.riverId == river.id" v-for="river in rivers">{{ river.name }}</option>
                  </select>
                  <span id="bean-riverId-error" class="help-block m-b-none" for="bean-riverId"></span>
                </div>
              </div>
            </div>
            <div class="clearfix "></div>
            <div class="col-md-12">
              <vue-area
                  areaId="areaId"
                  areaName="areaName"
                  :areaIdVal="item.areaId||staff.dto().areaId"
                  :areaNameVal="item.areaName||staff.dto().areaName"
                  :areaFixed="item.areaId||staff.dto().areaId"
                  :selectBy="item.areaId||staff.dto().areaId"
                  areaRequired="true,true,false,false,false"
              ></vue-area>
            </div>
            <div class="col-md-6">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">监督河道：</label>
                <div class="col-md-12">
                  <input id="bean-superviseRiverReach" name="superviseRiverReach" v-model="item.superviseRiverReach" class="form-control" type="text" placeholder="请输入监督河道" required="" aria-required="true" aria-describedby="bean-superviseRiverReach-error" maxlength="55" aria-invalid="true">
                  <span id="bean-superviseRiverReach-error" class="help-block m-b-none" for="bean-superviseRiverReach"></span>
                </div>
              </div>
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">联系电话：</label>
                <div class="col-md-12">
                  <input id="bean-cellphone" name="cellphone" v-model="item.cellphone" class="form-control" type="text" placeholder="请输入联系电话" required="" aria-required="true" aria-describedby="bean-cellphone-error" maxlength="25" aria-invalid="true">
                  <span id="bean-cellphone-error" class="help-block m-b-none" for="bean-cellphone"></span>
                </div>
              </div>
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">登录账号：</label>
                <div class="col-md-12">
                  <input id="bean-account" :disabled="newFlag!=0" name="account" v-model="item.account" class="form-control" type="text" placeholder="请输入登录名" required="" aria-required="true" aria-describedby="bean-account-error" maxlength="55" aria-invalid="true">
                  <span id="bean-account-error" class="help-block m-b-none" for="bean-account"></span>
                </div>
              </div>
            </div>
            <div class="col-md-6" v-if="newFlag==0">
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">初始密码：</label>
                <div class="col-md-12">
                  <input id="a_password" name="password" type="hidden"/>
                  <input id="bean-password" v-model="item.password" class="form-control" type="password" placeholder="请输入用户密码" required="" aria-required="true" aria-describedby="bean-password-error" maxlength="64" aria-invalid="true">
                  <span id="bean-password-error" class="help-block m-b-none" for="bean-password"></span>
                </div>
              </div>
              <div class="form-group clearfix">
                <label class="col-md-8 control-label">确认密码：</label>
                <div class="col-md-12">
                  <input id="a_confirmPassword" name="confirmPassword" type="hidden"/>
                  <input id="bean-confirmPassword" equalTo="#bean-password" v-model="item.confirmPassword" class="form-control" type="password" placeholder="请再次输入密码" required="" aria-required="true" aria-describedby="bean-confirmPassword-error" maxlength="64" aria-invalid="true">
                  <span id="bean-confirmPassword-error" class="help-block m-b-none" for="bean-confirmPassword"></span>
                </div>
              </div>
            </div>
            <div class="clearfix "></div>
            <div class="col-12 text-center" style="height: 50px;">
              <div>
                <input class="btn btn-primary" type="submit" value="保存"/> | <a @click="lbox.closeMyBoxLayer()" class="btn btn-danger" href="javascript:">返回</a>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
import '@/assets/css/style.css'

import $ from '@/assets/js/jquery-vendor.js'
import 'jquery.cookie'
import axios from 'axios'
import '@/assets/js/validate/validation-vendor.js'

import {apiUtil, axiosContentType, site} from '@/assets/js/boss'

import vueArea from '@/components/vue-area'

export default {
  components: {
    'vue-area': vueArea
  },
  data() {
    return {
      item: {},
      newFlag: 0,
      uuidToken: '',
      orgItems: [],
      rivers: []
    }
  },
  mounted() {
    let that = window.$vueApp = this;
    document.getElementsByTagName('body')[0].className = 'fixed-sidebar full-height-layout';
    $('#riverStaffForm').validate();
    that.staff.init(function () {
      that.newFlag = sessionStorage.getItem(site.riverStaff.info);
      if (that.newFlag) that.uuidToken = apiUtil.guid();
      if (apiUtil.existSessionKey(site.riverStaff.info)) {
        that.info();
      }
    });
    that.getOrglist();
    that.riverList();
  },
  methods: {
    info() {
      let that = this;
      axios.get(site.riverStaff.info + sessionStorage.getItem(site.riverStaff.info), {}).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          that.item = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    getOrglist() {
      let that = this;
      axios.post(site.orgInfo.list, {}).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          that.orgItems = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    riverList() {
      let that = this;
      axios.get(site.riverInfo.list, {}).then(function (response) {
        var result = response.data;
        if (result.code === 0) {
          that.rivers = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    save() {
      let that = this;
      if ($('#riverStaffForm').valid()) {
        that.staff.encrypt(function (encrypt) {
          $('#a_password').val(that.crypto.encrypt(encrypt.key, that.crypto.md5(that.item.password)));
          $('#a_confirmPassword').val(that.crypto.encrypt(encrypt.key, that.crypto.md5(that.item.confirmPassword)));
          let dataFormObj = $('#riverStaffForm').serializeObject();
          if (apiUtil.notEmpty(dataFormObj.riverId)) {
            dataFormObj.riverName = $('#bean-riverId').find('option:selected').text().replace(/[\r\n]/g, '');
          }
          dataFormObj.orgName = $('#bean-orgId').find('option:selected').text().replace(/[\r\n]/g, '');
          axios.post(site.riverStaff.save, $.param(dataFormObj, true)
              , axiosContentType.xWwwFormUrlencoded).then(function (response) {
            const result = response.data;
            if (result.code === 0) {
              parent.$vueApp.page();
              alert('保存成功!');
              that.lbox.closeMyBoxLayer()
            } else {
              alert(result.message);
            }
          });
        });
      }
    }
  }
}
</script>
